package de.gwtprime.client.widgets.popup;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class SocialMediaLoginForm extends Composite{
	
	interface SocialMediaLoginFormUIBinder extends UiBinder<Widget, SocialMediaLoginForm>{};
	SocialMediaLoginFormUIBinder uiBinder = GWT.create(SocialMediaLoginFormUIBinder.class);
	
	
	@UiField Button facebookLogin;
	@UiField Button googleLogin;
	
	public SocialMediaLoginForm() {

		initWidget(uiBinder.createAndBindUi(this));
		
		
		facebookLogin.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.assign("/socialauth"+getLastPageParameter());
				
			}
		});
		
		googleLogin.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
						
				Window.Location.assign("/logingoogle"+getLastPageParameter());
				
			}
		});
	}
	
	
	/**
	 * Get the current GWT location (e.g.: #!Article:1). This is passed to the Google/Socialmedia-Login servlet
	 * and stored in the session there. After the login the hash is appended again to the url to come back to last
	 * visited site
	 * @return ?lastPage=!Article:1 (Without the # because it is not transmitted otherwise)
	 */
	public String getLastPageParameter(){
		return "?lastPage="+Window.Location.getHash().replace("#", "");
	}
	
	
	
}
